package edu.cmu.ece.ds.droidchat;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.model.StringConstants;
import edu.cmu.ece.ds.droidchat.preference.ColorPreference;
import edu.cmu.ece.ds.droidchat.preference.ProfilePicturePreference;

public class SettingsActivity extends PreferenceActivity {
    private static final String TAG = "SettingsActivity";
    private final int SELECT_IMAGE = 0;

    private ListView mListView;
    private ImageView mProfilePictureImageView;

    private Drawable mProfilePicture;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        mListView = getListView();
        mListView.setTextFilterEnabled(true);
        mListView.setDivider(null);
        mListView.setBackgroundColor(Color.TRANSPARENT);
        mListView.setCacheColorHint(Color.WHITE);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
        String key = preference.getKey();
        Log.d(TAG, "key=" + key);
        if (key.equals(StringConstants.PREF_KEY_ABOUT_DROIDCHAT)) {
            Log.d(TAG, "HELLO!");
            Toast.makeText(getApplicationContext(), R.string.authors,
                    Toast.LENGTH_SHORT).show();

        } else if (key
                .equals(StringConstants.PREF_KEY_MY_MESSAGE_BACKGROUND_COLOR)) {
            Log.e(TAG, "myMessageBackgroundColor onClick");
        } else if (key.equals(StringConstants.PREF_KEY_MY_PROFILE_PICTURE)) {
            ProfilePicturePreference pref = (ProfilePicturePreference) preference;
            mProfilePictureImageView = pref.getProfilePictureImageView();
            startActivityForResult(
                    new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
                    SELECT_IMAGE);

        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_IMAGE) {
            if (resultCode == RESULT_OK) {
                Uri photoUri = data.getData();

                if (photoUri != null) {
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                                this.getContentResolver(), photoUri);

                        Log.e(TAG,
                                "bitmap " + bitmap.getWidth() + ","
                                        + bitmap.getHeight());

                        int newWidth, newHeight;
                        double ratio = (double) bitmap.getWidth()
                                / bitmap.getHeight();
                        if (ratio >= 1) {
                            newWidth = 60;
                            newHeight = (int) (60 / ratio);
                        } else {
                            newWidth = (int) (60 / ratio);
                            newHeight = 60;
                        }
                        bitmap = Bitmap.createScaledBitmap(bitmap, newWidth,
                                newHeight, true);
                        mProfilePictureImageView.setImageBitmap(bitmap);
                        mProfilePicture = mProfilePictureImageView
                                .getDrawable();
                        Log.e(TAG, "mProfilePicture=" + mProfilePicture);
                    } catch (OutOfMemoryError e) {
                        Toast.makeText(this, R.string.out_of_memory,
                                Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
